package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysFileEntity;
import com.ruoyi.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController {
    @Autowired
    private ISysFileService fileService;

    /**
     * 获取部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public AjaxResult list(SysFileEntity file) {
        List<SysFileEntity> list = fileService.list();
        return AjaxResult.success(list);
    }


    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{fileID}")
    public AjaxResult getInfo(@PathVariable Long fileID) {
        return AjaxResult.success(fileService.getById(fileID));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sysFileId}")
    public AjaxResult remove(@PathVariable Long sysFileId) {
        SysFileEntity file = fileService.getById(sysFileId);
        FileUtils.deleteFile(file.getFilePath());
        return toAjax(fileService.removeById(sysFileId));
    }
}
